home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Eudora 1.3.1 / source / progress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  9.4 KB  |  333 lines  |  [TEXT/MPS ]

  1. #define FILE_NUM 33
  2. /* Copyright (c) 1990-1992 by the University of Illinois Board of Trustees */
  3. /************************************************************************
  4.  * Progress monitoring software
  5.  ************************************************************************/
  6. #pragma load EUDORA_LOAD
  7. #pragma segment Progress
  8.  
  9. /************************************************************************
  10.  * other stuff
  11.  ************************************************************************/
  12. typedef struct {
  13.     short percent;
  14.     Str255 message;
  15.     int on, total;
  16.     short n;
  17.     Handle next;
  18. } ProgressBlock, *ProgressBPtr, **ProgressBHandle;
  19. Boolean ProgressOff;
  20.  
  21. MyWindowPtr ProgWindow;
  22.  
  23. /************************************************************************
  24.  * Private functions
  25.  ************************************************************************/
  26. void ProgressUpdate(MyWindowPtr win);
  27. void DisposProgress(ProgressBHandle prbl);
  28. void CenterProgress(MyWindowPtr win);
  29. void InvalProgress(Boolean message,Boolean percent);
  30. void DrawProgressBar(void);
  31.  
  32. /************************************************************************
  33.  * ByteProgress - keep track of the number of bytes transmitted so far
  34.  ************************************************************************/
  35. void ByteProgress(UPtr message, int onLine, int totLines)
  36. {
  37.     ProgressBlock **prbl;
  38.     
  39.     CycleBalls();
  40.     if (!ProgWindow) return;
  41.     
  42.     prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  43.     
  44.     if (onLine>=0) (*prbl)->on = onLine;
  45.     else (*prbl)->on -= onLine;
  46.     if (totLines) (*prbl)->total = totLines;
  47.     
  48.     if (!(*prbl)->total) (*prbl)->total = 1;
  49.     Progress(((*prbl)->on*100)/(*prbl)->total,message);
  50. }
  51.  
  52. /************************************************************************
  53.  * OpenProgress - create the progress window
  54.  ************************************************************************/
  55. int OpenProgress(void)
  56. {
  57.     int err;
  58.     ProgressBlock **prbl;
  59.     MyWindowPtr win;
  60.     
  61.     if (ProgWindow || PrefIsSet(PREF_NO_PROGRESS)) return(0);
  62.     if ((win=GetNewMyWindow(PROGRESS_WIND,nil,InFront,False,False))==nil)
  63.     {
  64.         WarnUser(COULDNT_WIN,err=MemError());
  65.         return(err);
  66.     }
  67.     
  68.     InsetRect(&win->contR,8,8);
  69.     
  70.     prbl = NewZHandle(sizeof(ProgressBlock));
  71.     if (!prbl) {WarnUser(MEM_ERR,err=MemError()); DisposeWindow(win); return(err);}
  72.     
  73.     ((WindowPeek)win)->refCon = (long) prbl;
  74.     win->update = ProgressUpdate;
  75.     (*prbl)->percent = -1;
  76.     SetPort(win);
  77.     TextFont(0);
  78.     TextSize(12);
  79.     ProgWindow = win;
  80.     win->isRunt = True;
  81.     if (InBG) DisableProgress(); else CenterProgress(ProgWindow);
  82.     ShowWindow(win);
  83.     return(noErr);
  84. }
  85.  
  86. /************************************************************************
  87.  * CloseProgress - close the progress window
  88.  ************************************************************************/
  89. void CloseProgress(void)
  90. {
  91.     if (!ProgWindow) return;
  92.     DisposProgress((Handle)((WindowPeek)ProgWindow)->refCon);
  93.     CloseMyWindow(ProgWindow);
  94.     ProgWindow = nil;
  95. }
  96.  
  97. /************************************************************************
  98.  * DisposProgress - get rid of the progress chain
  99.  ************************************************************************/
  100. void DisposProgress(ProgressBHandle prbl)
  101. {
  102.     if (prbl==nil) return;
  103.     DisposProgress((*prbl)->next);
  104.     DisposHandle(prbl);
  105. }
  106.  
  107. /************************************************************************
  108.  * Progress - record progress in the progress window
  109.  ************************************************************************/
  110. void Progress(short percent,UPtr message)
  111. {
  112.     ProgressBlock **prbl;
  113.     GrafPtr oldPort;
  114.     Str255 scratch;
  115.     
  116.     if (!ProgWindow) return;
  117.     prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  118.     if (percent!=NoChange && percent!=(*prbl)->percent || message)
  119.     {
  120.         if (percent!=NoChange && percent!=(*prbl)->percent)
  121.         {
  122.           if ((*prbl)->percent == NoBar || percent==NoBar)
  123.             {
  124.                 InvalContent(ProgWindow);
  125.                 (*prbl)->percent = percent;
  126.             }
  127.             else
  128.             {
  129.                 (*prbl)->percent = percent;
  130.                 DrawProgressBar();
  131.             }
  132.         }
  133.         if (message)
  134.         {
  135.             register UPtr to,from;
  136.             register unsigned short count;
  137.             for (to=scratch+1,from=message+1,count= *message;
  138.                      count; count--,from++)
  139.             {
  140.                 if (*from >= ' ') *to++ = *from;
  141.             }
  142.  
  143.             *scratch = to - scratch - 1;
  144.             if (!EqualString(scratch,LDRef(prbl)->message,False,True))
  145.             {
  146.                 PCopy((*prbl)->message,scratch);
  147.                 InvalProgress(True,False);
  148.                 Log(LOG_PROG,LDRef(prbl)->message);
  149.             }
  150.             UL(prbl);
  151.         }
  152.         GetPort(&oldPort);
  153.     if (FrontWindow()!=ProgWindow)
  154.             SelectWindow(ProgWindow);
  155.         if (InBG) DisableProgress(); else EnableProgress();
  156.         UpdateMyWindow(ProgWindow);
  157.     }
  158. }
  159.  
  160. /************************************************************************
  161.  * InvalProgress - invalidate the selected part of the progress window
  162.  ************************************************************************/
  163. void InvalProgress(Boolean message,Boolean percent)
  164. {
  165.     Rect messageRect,percentRect;
  166.     ProgressBlock **prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  167.     SAVE_PORT;
  168.     SetPort(ProgWindow);
  169.     messageRect = percentRect = ProgWindow->contR;
  170.     if ((*prbl)->percent >= 0)
  171.         messageRect.bottom=percentRect.top = (percentRect.top+percentRect.bottom)/2;
  172.     if (message) InvalRect(&messageRect);
  173.     if (percent) InvalRect(&percentRect);
  174.     REST_PORT;
  175. }
  176.  
  177.  
  178. /************************************************************************
  179.  * ProgressUpdate - update the progress window
  180.  ************************************************************************/
  181. void ProgressUpdate(MyWindowPtr win)
  182. {
  183.     Rect aRect;
  184.     ProgressBlock **prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  185.     int width;
  186.     int h,v, wl, wr, wt, wb;
  187.         
  188.     aRect = win->contR;
  189.     InsetRect(&aRect,-6,-6);
  190.     ClipRect(&aRect);
  191.     PenSize(2,2);
  192.     if (InBG) PenPat(&qd.gray);
  193.     FrameRect(&aRect);
  194.     PenNormal();
  195.     
  196.     ClipRect(&win->contR);
  197.     wl = win->contR.left; wr = win->contR.right;
  198.     wt = win->contR.top; wb = win->contR.bottom;
  199.     width = StringWidth(LDRef(prbl)->message); UL(prbl);
  200.     h = (wl+wr-width)/2;
  201.     v = (*prbl)->percent < 0 ? wb : (wb+wt)/2;
  202.     SetRect(&aRect,wl,wt,wr,v);
  203.     EraseRect(&aRect);
  204.     MoveTo(h,wt + (v-wt)/2+4);
  205.     DrawString(LDRef(prbl)->message); UL(prbl);
  206.     
  207.     if ((*prbl)->percent >= 0) DrawProgressBar();
  208. }
  209.  
  210.  
  211. /************************************************************************
  212.  * 
  213.  ************************************************************************/
  214. void DrawProgressBar(void)
  215. {
  216.     Rect aRect;
  217.     ProgressBlock **prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  218.     short wr;
  219.     SAVE_PORT;
  220.     SetPort(ProgWindow);
  221.         
  222.     aRect = ProgWindow->contR;
  223.     InsetRect(&aRect,-6,-6);
  224.     aRect.top = (aRect.top+aRect.bottom)/2;
  225.     wr = aRect.right;
  226.     aRect.right = ((wr-aRect.left)*(*prbl)->percent)/100;
  227.     aRect.right = MIN(wr,aRect.right);
  228.     FillRect(&aRect,&qd.black);
  229.     aRect.left = aRect.right;
  230.     aRect.right = wr;
  231.     FillRect(&aRect,&qd.gray);
  232.     REST_PORT;
  233. }
  234.  
  235. /************************************************************************
  236.  * DisableProgress - make the progress calls do nothing
  237.  ************************************************************************/
  238. void DisableProgress(void)
  239. {
  240.     if (ProgWindow)
  241.         MoveWindow(ProgWindow,INFINITY/2,INFINITY/2,False);
  242.     ProgressOff = True;
  243. }
  244.  
  245. /************************************************************************
  246.  * EnableProgress - turn the progress calls back on
  247.  ************************************************************************/
  248. void EnableProgress(void)
  249. {
  250.     if (ProgressOff && ProgWindow)
  251.     {
  252.         CenterProgress(ProgWindow);
  253.         UpdateMyWindow(ProgWindow);
  254.     }
  255.     ProgressOff = False;
  256. }
  257.  
  258. /************************************************************************
  259.  * PushProgress - stash a copy of the progress info
  260.  ************************************************************************/
  261. void PushProgress(void)
  262. {
  263.     ProgressBHandle prbl;
  264.     ProgressBHandle pH;
  265.     
  266.     if (!ProgWindow) return;
  267.     prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  268.     if (pH=NuHandle(sizeof(ProgressBlock)))
  269.     {
  270.         **pH = **prbl;
  271.         (*pH)->next = prbl;
  272.         ((WindowPeek)ProgWindow)->refCon = (long) pH;
  273.     }
  274. }
  275.  
  276. /************************************************************************
  277.  * PopProgress - restore the progress info
  278.  ************************************************************************/
  279. void PopProgress(Boolean messageOnly)
  280. {
  281.     ProgressBHandle prbl;
  282.     ProgressBHandle pNext;
  283.     
  284.     if (!ProgWindow) return;
  285.     prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  286.     if ((*prbl)->next)
  287.     {
  288.         pNext = (*prbl)->next;
  289.         if (messageOnly)
  290.         {
  291.             (*pNext)->percent = (*prbl)->percent;
  292.             (*pNext)->on = (*prbl)->on;
  293.             (*pNext)->total = (*prbl)->total;
  294.             InvalProgress(True,False);
  295.         }
  296.       else InvalContent(ProgWindow);
  297.             
  298.         ((WindowPeek)ProgWindow)->refCon = (long) pNext;
  299.         DisposHandle(prbl);
  300.         UpdateMyWindow(ProgWindow);
  301.     }
  302. }
  303.     
  304. /************************************************************************
  305.  *
  306.  ************************************************************************/
  307. void SetProgressN(short n)
  308. {
  309.     ProgressBlock **prbl;
  310.     if (!ProgWindow) return;
  311.     prbl = (ProgressBlock **)((WindowPeek)ProgWindow)->refCon;
  312.     (*prbl)->n = n;
  313. }
  314.  
  315. /************************************************************************
  316.  *
  317.  ************************************************************************/
  318. void CenterProgress(MyWindowPtr win)
  319. {
  320.     Rect r = ((GrafPtr)win)->portRect;
  321.     Rect screenR = qd.screenBits.bounds;
  322.     TopCenterRectIn(&r,&screenR);
  323.     MoveWindow(win,r.left,r.top+GetMBarHeight()+1,False);
  324. }
  325.  
  326. /************************************************************************
  327.  * ProgressIsOpen - is the progress window open?
  328.  ************************************************************************/
  329. Boolean ProgressIsOpen(void)
  330. {
  331.     return(ProgWindow||PrefIsSet(PREF_NO_PROGRESS));
  332. }
  333.